package com.hj.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hj.pojo.User;

@RestController
@RequestMapping("/user")
public class UserController {

	List<User> userList = new java.util.concurrent.CopyOnWriteArrayList<>();
	
	public UserController() {
		userList.add(new User("1", "admin", "nan", "18"));
	}
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@GetMapping
	public Object findAll() {
		return jdbcTemplate.queryForList("select * from category");
	}
	
	@GetMapping("/{id}")
	public User findOne(@PathVariable("id") String id) {
		Optional<User> optional = userList.stream().filter((u) -> u.getId().equals(id)).findFirst();
		return optional.get();
	}
	
	@DeleteMapping("/{id}")
	public Object delete(@PathVariable("id") String id) {
		
		for (User user : userList) {
			if (user.getId().equals(id)) {
				userList.remove(user);
			}
		}
		
		return "del ok";
	}
	
	@PostMapping
	public Object add() {
		//userList.add(user);
		return "add ok";
	}
	
	@PutMapping
	public Object upate(String name) {
		//userList.add(user);
		return "update ok";
	}
	
}
